layui.use(['layer', 'form', 'tree'], function () {
    var layer = layui.layer,
        $ = layui.jquery,
        tree = layui.tree,
        form = layui.form;

    function getData(){
        var data ;
        $.ajax({
            url:"/server/getAllFolder",
            type:"get",
            async:false,
            success:function(result){
                data=result;
            }
        });
        return data;
    };
    function getDataList(){
        var data ;
        $.ajax({
            url:"/server/list",
            type:"get",
            async:false,
            success:function(result){
                data=result;
            }
        });
        return data;
    };

    tree.render({
        elem: "#classtree",
        data: getData(),
        click: function (node) {
            var $select = $($(this)[0].elem).parents(".layui-form-select");
            $select.removeClass("layui-form-selected").find(".layui-select-title span").html(node.data.title).end().find("input:hidden[name='parentId']").val(node.data.id);
        }
    });
    $(".downpanel").on("click", ".layui-select-title", function (e) {
        $(".layui-form-select").not($(this).parents(".layui-form-select")).removeClass("layui-form-selected");
        $(this).parents(".downpanel").toggleClass("layui-form-selected");
        layui.stope(e);
    }).on("click", "dl i", function (e) {
        layui.stope(e);
    });
    $(document).on("click", function (e) {
        $(".layui-form-select").removeClass("layui-form-selected");
    });

    $(function () {
        setTimeout(function() {
            var type = $("#type").val();
            $(".type input[value="+type+"]").attr("checked","checked");
            if (type == 0) {
                hide();
            }else {
                show();
            }
            selectShow()
            form.render();
        },100)

    });


    function selectShow(){
        const data = getDataList();
        var parentId = $("#parentId").val();
        for (item of data){
            if (parentId == item.id && item.type==0){
                $(".layui-select-title span").html(item.name);
                break
            }
            if (parentId ==  -1){
                $(".layui-select-title span").html("根目录")
                break
            }
        }
    }

    function hide() {
        $(".vpn").hide();
        $(".ip").hide();
        $(".username").hide();
        $(".password").hide();
        $(".port").hide();
        form.render();
    }
    function show() {
        $(".vpn").show();
        $(".ip").show();
        $(".username").show();
        $(".password").show();
        $(".port").show();
    }
    form.on('radio(type)', function (data) {
        if (data.value == 0) {
            hide();
        } else if (data.value == 1) {
            $("#layui-layer2").height(900)
            show();
        }
        form.render();
    });

    //提交监听事件
    form.on('submit(save)', function (data) {
        const type = $("input[type=radio]:checked").val();
        const params = data.field;
        if (type==1) {
            if (params.vpn == null || params.vpn == ""){
                layer.msg("vpn必填", {icon: 0, time: 3000})
                return false;
            }
            if (params.parentId == null || params.parentId == ""){
                layer.msg("目录必选", {icon: 0, time: 3000})
                return false;
            }
            if (params.ip == null || params.ip == ""){
                layer.msg("ip必填", {icon: 0, time: 3000})
                return false;
            }
            if (params.username == null || params.username == ""){
                layer.msg("账户必填", {icon: 0, time: 3000})
                return false;
            }
            if (params.password == null || params.password == ""){
                layer.msg("密码必填", {icon: 0, time: 3000})
                return false;
            }
            if (params.port == null || params.port == ""){
                layer.msg("端口必填", {icon: 0, time: 3000})
                return false;
            }
        }
        submit($, params);
        return false;
    })
    $("#closeBtn").on("click", function (e) {
        CloseWin()
    });

    //提交
    function submit($, params) {
        $.ajax({
            type: "PUT",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "/server",//url
            data: JSON.stringify(params),
            contentType: "application/json;charset=utf-8",
            success: function (res) {
                if (res.code == 0) {
                    layer.msg("修改成功", {icon: 1}, function () {
                        CloseWin();
                    })
                } else {
                    layer.msg(res.desc, {icon: 0}, function () {
                        // location.reload(); // 页面刷新
                        return false
                    })
                }
            },
            error: function () {
                alert("异常！");
            }
        });
    }

    //关闭页面
    function CloseWin() {
        parent.location.reload(); // 父页面刷新
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }


});

